package com.learning.platform.controller;

import com.learning.platform.dto.ApiResponse;
import com.learning.platform.dto.PostRequest;
import com.learning.platform.dto.PostResponse;
import com.learning.platform.dto.CommentRequest;
import com.learning.platform.dto.CommentResponse;
import com.learning.platform.enums.PostType;
import com.learning.platform.security.UserPrincipal;
import com.learning.platform.service.CommunityService;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/community")
public class CommunityController {
    
    private final CommunityService communityService;
    
    public CommunityController(CommunityService communityService) {
        this.communityService = communityService;
    }
    
    // 帖子相关API
    @PostMapping("/posts")
    public ApiResponse<PostResponse> createPost(@Valid @RequestBody PostRequest request, Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        PostResponse response = communityService.createPost(request, principal.getUserId());
        return ApiResponse.success(response, "发布成功");
    }
    
    @GetMapping("/courses/{courseId}/posts")
    public ApiResponse<Page<PostResponse>> getPostsByCourse(
            @PathVariable Long courseId,
            @RequestParam(required = false) PostType postType,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        
        Page<PostResponse> posts = communityService.getPostsByCourse(
            courseId, postType, keyword, PageRequest.of(page - 1, pageSize));
        return ApiResponse.success(posts);
    }
    
    @GetMapping("/posts/{id}")
    public ApiResponse<PostResponse> getPost(@PathVariable Long id) {
        PostResponse response = communityService.getPost(id);
        return ApiResponse.success(response);
    }
    
    @PutMapping("/posts/{id}")
    public ApiResponse<PostResponse> updatePost(@PathVariable Long id,
                                               @Valid @RequestBody PostRequest request,
                                               Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        PostResponse response = communityService.updatePost(id, request, principal.getUserId());
        return ApiResponse.success(response, "更新成功");
    }
    
    @DeleteMapping("/posts/{id}")
    public ApiResponse<Void> deletePost(@PathVariable Long id, Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        communityService.deletePost(id, principal.getUserId());
        return ApiResponse.success(null, "删除成功");
    }
    
    @PostMapping("/posts/{id}/like")
    public ApiResponse<Void> likePost(@PathVariable Long id, Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        communityService.toggleLike("POST", id, principal.getUserId());
        return ApiResponse.success(null, "操作成功");
    }
    
    // 评论相关API
    @PostMapping("/posts/{postId}/comments")
    public ApiResponse<CommentResponse> createComment(@PathVariable Long postId,
                                                     @Valid @RequestBody CommentRequest request,
                                                     Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        request.setPostId(postId);
        CommentResponse response = communityService.createComment(request, principal.getUserId());
        return ApiResponse.success(response, "评论成功");
    }
    
    @GetMapping("/posts/{postId}/comments")
    public ApiResponse<Page<CommentResponse>> getCommentsByPost(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        
        Page<CommentResponse> comments = communityService.getCommentsByPost(
            postId, PageRequest.of(page - 1, pageSize));
        return ApiResponse.success(comments);
    }
    
    @PostMapping("/comments/{id}/like")
    public ApiResponse<Void> likeComment(@PathVariable Long id, Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        communityService.toggleLike("COMMENT", id, principal.getUserId());
        return ApiResponse.success(null, "操作成功");
    }
    
    @PostMapping("/comments/{id}/solution")
    public ApiResponse<Void> markAsSolution(@PathVariable Long id, Authentication auth) {
        UserPrincipal principal = (UserPrincipal) auth.getPrincipal();
        communityService.markAsSolution(id, principal.getUserId());
        return ApiResponse.success(null, "已标记为解答");
    }
}

